local skill = fk.CreateSkill{
  name = "#lvshi__shield",
}

Fk:loadTranslationTable{
  ["#lvshi__shield"] = "护甲",
  ["shield"] = "护甲",
  ["@@shield"] = "护甲",
  ["#lvshishield_skill"] = "护甲",
  ["#shield-invoke"] = "护甲：请弃置 %arg 张护甲抵消 %arg 点伤害",
  ["#shield_remove"] = "%from 移除了护甲：%arg",
  ['lvs__shield'] = "当你受到伤害时，若你有护甲，则你弃置等同于此伤害点数的护甲，防止等量伤害；准备阶段，你获得所有护甲",
}

skill:addEffect(fk.DamageInflicted, {
  global = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and #player:getPile("shield") > 0 and data.skillName ~= "fire__slash_skill"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if #player:getPile("shield") > data.damage then
      local n = data.damage
      local params = {
        min_num = n,
        max_num = n,
        include_equip = false,
        skill_name = skill.name,
        cancelable = false,
        pattern = ".|.|.|shield|.|.",
        prompt = "#shield-invoke:::"..n,
        expand_pile = "shield",
        }
      local card = room:askToCards(player, params)
      if #card == 0 then card = table.random(player:getPile("shield"), n) end
      room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "", "", true)
      room.logic:trigger("fk.ShieldCosted", player, {ids = card, from = data.from or nil})
      data:preventDamage()
    else
      data.damage = data.damage - #player:getPile("shield")
      local ids = player:getPile("shield")
      room:moveCardTo(ids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "", "", true)
      room.logic:trigger("fk.ShieldCosted", player, {ids = ids, from = data.from or nil})
    end
  end,
})

skill:addEffect(fk.EventPhaseStart, {
  global = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and target.phase == Player.Start and #target:getPile("shield") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:moveCardTo(player:getPile("shield"), Card.PlayerHand, player, fk.ReasonJustMove, skill.name, "", true)
  end,
})

return skill